package com.ez.supervise.model.sys;

import java.util.ArrayList;
import java.util.List;

import com.ez.supervise.model.base.BaseEntity;

public class Permission extends BaseEntity {

    private static final long serialVersionUID = 4357292670220999809L;

    private String resId;// 权限id（可以参考到资源表）
    private String resName;// 权限名称
    private String resType;// 权限类型
    private String resUrl;// 访问url
    private String remark;

    public static final String TYPE_MAIN_MENU = "main_menu";
    public static final String TYPE_SUB_MENU = "sub_menu";
    public static final String TYPE_OPERATION = "operation";

    public Permission() {
	super();
    }

    public String getResId() {
	return resId;
    }

    public void setResId(String resId) {
	this.resId = resId;
    }

    public String getResName() {
	return resName;
    }

    public void setResName(String resName) {
	this.resName = resName;
    }

    public String getResType() {
	return resType;
    }

    public void setResType(String resType) {
	this.resType = resType;
    }

    public String getResUrl() {
	return resUrl;
    }

    public void setResUrl(String resUrl) {
	this.resUrl = resUrl;
    }

    public void setRemark(String remark) {
	this.remark = remark;
    }

    public String getRemark() {
	return remark;
    }

    public String getPanel() {
	int pos = resId.lastIndexOf('_');
	if(pos > -1){
	    return resId.substring(pos + 1);
	} 
	return resId;
    }
    
    public static List<Permission> filter(List<Permission> permissionList, String type) {
	List<Permission> result = new ArrayList<Permission>();
	for (Permission permission : permissionList) {
	    if (permission != null && type.equalsIgnoreCase(permission.getResType())) {
		result.add(permission);
	    }
	}
	return result;
    }

    public static List<Permission> children(Permission parent, List<Permission> permissionList) {
	List<Permission> result = new ArrayList<Permission>();
	if (parent == null || permissionList == null || permissionList.isEmpty()) {
	    return result;
	}
	String parentId = parent.getResId();
	for (Permission permission : permissionList) {
	    if (permission != null && permission != parent && permission.getResId().startsWith(parentId)) {
		result.add(permission);
	    }
	}
	return result;
    }
}
